Page({
  // 页面的初始数据
  data: {
      changeusername: "",
      changegender: "",
      changebirthday: "",
      gender: "",
      username: "",
      phone: "",
      birthday: "",
      manchecked: 0,
      womanchecked: 0
  },
  // 生命周期函数--监听页面加载
  onLoad: function (options) {
    this.getAll();
  },
  // 生命周期函数--监听页面初次渲染完成
  onReady: function () {
  },
  // 更新性别选择
  updateRadio: function (e) {
    if(e.detail.value == "man") {
      this.setData({
       changegender:"男"
      })
    } else {
      this.setData({
        changegender:"女"
      }) 
    }
 },
 // 更新信息
 changeInfo: function (e) {
    this.setData({
      changeusername: e.detail.value.changeusername,
      changebirthday: e.detail.value.changebirthday
    })
    if(e.detail.value.changeusername == "") {
      wx.showToast({
        title: '用户名不能为空，请重新输入！',
        icon: "none"
      })
    } else {
      wx.request({
        url: 'http://192.168.166.182:8080/changeUser',
        data: { username:this.data.changeusername, gender:this.data.changegender, birthday:this.data.changebirthday, phone:this.data.phone },
        success: (result) => {
          if(result.data == "修改成功") {
            //重新获取信息
            this.getAll();
            wx.showToast({
              title: '修改成功',
              icon: "success"
            })
            // 跳转回原先页面
            setTimeout(function () {
              wx.navigateBack()
            }, 1000)
          } else {
            wx.showToast({
            title: result.data,
            icon:"none"
          })
          }
        }
      })
    }
  },
  // 生命周期函数--监听页面显示
  onShow: function () {
    
  },
  // 得到原有的所有数据
  getAll:function(){
    this.setData({
      phone: wx.getStorageSync('phone')
    })
    wx.request({
      url: 'http://192.168.166.182:8080/findByPhone',
      data: { phone:this.data.phone },
      success:(result)=>{
        console.log(result)
        wx.setStorageSync('gender', result.data.gender)
        wx.setStorageSync('username', result.data.username)
        wx.setStorageSync('birthday', result.data.birthday)
        this.setData({
          username:wx.getStorageSync('username'),
          gender:wx.getStorageSync('gender'),
          birthday:wx.getStorageSync('birthday'),
        })
        if(this.data.gender=="男"){
          this.setData({
            manchecked:1
          })
        }
        if(this.data.gender=="女"){
          this.setData({
            womanchecked:1
          })
        }
      }
    })
  },
  // 生命周期函数--监听页面隐藏
  onHide: function () {

  },
  // 生命周期函数--监听页面卸载
  onUnload: function () {

  },
  // 页面相关事件处理函数--监听用户下拉动作
  onPullDownRefresh: function () {

  },
  // 页面上拉触底事件的处理函数
  onReachBottom: function () {

  },
  // 用户点击右上角分享
  onShareAppMessage: function () {

  }
})